package com.example.spotitube.View;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.spotitube.R;
import com.example.spotitube.Connection.Connection;
import com.example.spotitube.Model.Track;


/**
 * @author Rick
 *
 *	This activity shows the details of a specific track
 *
 */
public class TrackDetailActivity extends Activity implements OnClickListener{
	
	private Track	track;
	
	
	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		
		setContentView( R.layout.track_detail );
		
		track = ( Track ) getIntent().getSerializableExtra( "track" );
		
		TextView trackName = ( TextView ) findViewById( R.id.trackName );
		trackName.setText( track.getTrackName() );
		
		TextView releasedOn = ( TextView ) findViewById( R.id.releasedIn );
		releasedOn.setText( ( CharSequence ) ( "" + track.getYearOfRelease() ) );
		
		TextView albumName = ( TextView ) findViewById( R.id.fromAlbum );
		albumName.setText( ( CharSequence ) ( track.getAlbumName() ) );
		
		ImageButton playSong = ( ImageButton ) findViewById( R.id.playSongButton );
		playSong.setOnClickListener( this );
		
		Button showAlbum = ( Button ) findViewById( R.id.toAlbumButton );
		showAlbum.setOnClickListener( this );
		
		int minutes = track.getLength() / 60;
		int seconds = track.getLength() % 60;
		TextView trackLength = ( TextView ) findViewById( R.id.lengthOf );
		trackLength.setText( ( CharSequence ) ( "" + minutes + " minute " + seconds ) );
		
	}
	
	
	@Override
	public void onClick( View v ) {
		switch( v.getId() ){
			case R.id.playSongButton:
				Intent i = new Intent( Intent.ACTION_VIEW );
				
				try{
					JSONObject youtubeJson = Connection
							.getJson( "http://gdata.youtube.com/feeds/api/videos?alt=json&orderby=relevance&v=2&q="
									+ track.getEncodedSearch() + "&safeSearch=none&time=all_time" );
					JSONObject feed = youtubeJson.getJSONObject( "feed" );
					JSONArray entry = feed.getJSONArray( "entry" );
					JSONObject entry1 = ( JSONObject ) entry.get( 0 );
					JSONObject media$group = entry1.getJSONObject( "media$group" );
					JSONObject url = media$group.getJSONObject( "media$player" );
					String realUrl = url.getString( "url" );
					i.setData( Uri.parse( realUrl ) );
					startActivity( i );
				} catch( Exception e ){
					System.out.println( "Json error!" );
					e.printStackTrace();
					
				}
				break;
			case R.id.toAlbumButton:
				Intent intent = new Intent( this, AlbumDetailActivity.class );
				intent.putExtra( "albumName", track.getAlbumName() );
				startActivity( intent );
				break;
			default:
				break;
		}
		
	}
}
